package cn.tedu.ivos.user.service.impl;

import cn.tedu.ivos.base.exception.ServiceException;
import cn.tedu.ivos.base.response.StatusCode;
import cn.tedu.ivos.user.mapper.UserMapper;
import cn.tedu.ivos.user.pojo.dto.UserLoginParam;
import cn.tedu.ivos.user.pojo.dto.UserQuery;
import cn.tedu.ivos.user.pojo.dto.UserSaveParam;
import cn.tedu.ivos.user.pojo.entity.User;
import cn.tedu.ivos.user.pojo.vo.UserVO;
import cn.tedu.ivos.user.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.Date;
import java.util.List;

/**
业务层实现类需要添加@Service注解，表示当前类为业务层实现类
 */
@Slf4j
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;

    @Override
    public UserVO login(UserLoginParam userLoginParam) {
        UserVO userVO = userMapper.selectByUsername(userLoginParam.getUsername());
        if(userVO == null){
            throw new ServiceException(StatusCode.USERNAME_ERROR);
        }
        if(!userVO.getPassword().equals(userLoginParam.getPassword())){
            throw new ServiceException(StatusCode.PASSWORD_ERROR);
        }
        return userVO;
    }

    @Override
    public void saveUser(UserSaveParam userSaveParam) {
        log.debug("保存用户的业务: userSaveParam={}" , userSaveParam);
        /**
         * 根据在UserSaveParam中预留的id属性，判断出当前操作是新增用户还是修改编辑用户
         * 1.如果是新增用户id属性值是没有的
         * 2.如果是编辑用户肯定需要用户id值进行编辑
         */
        User user = new User();
        BeanUtils.copyProperties(userSaveParam,user);
        if(userSaveParam.getId() == null){
            user.setPassword("123456");
            user.setCreateTime(new Date());
            userMapper.insert(user);
        }else {
            //TODO:编辑用户操作
            user.setUpdateTime(new Date());
            userMapper.update(user);
        }
    }

    @Override
    public List<UserVO> selectUser(UserQuery userQuery) {
        log.debug("查询用户的业务: userQuery={}" , userQuery);
        List<UserVO> list = userMapper.selectUser(userQuery);
        log.debug("查询结果: list={}" , list);
        return list;
    }

    @Override
    public void resetPassword(Long userId) {
        log.debug("修改密码业务: userId={} ", userId);
        /**
         * new User()对象用来保存我们需要更新的信息
         * 比如密码，更新时间，更新对象的userId
         */
        User user = new User();
        user.setPassword("root");
        user.setId(userId);
        user.setUpdateTime(new Date());
        userMapper.update(user);
    }

    @Override
    public void updateStatus(Long userId, String status) {
        log.debug("修改用户状态业务: userId={},status={}" , userId,status);
        User user = new User();
        user.setId(userId);
        user.setStatus(status);
        user.setUpdateTime(new Date());
        userMapper.update(user);
    }

    @Override
    public void deleteUser(Long userId) {
        log.debug("删除用户业务: userId={}" , userId);
        userMapper.deleteUser(userId);
    }
}
